home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / C / x01c.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  246 lines

  1. /* $Id: x01c.c,v 1.11 1994/08/05 22:24:05 mjl Exp $
  2.  * $Log: x01c.c,v $
  3.  * Revision 1.11  1994/08/05  22:24:05  mjl
  4.  * Made new test code a bit more robust and unobtrusive.
  5.  *
  6.  * Revision 1.10  1994/07/29  20:13:02  mjl
  7.  * Added cursor-reporting test code at the end of the page.
  8.  * Contributed by Paul Casteels.
  9.  *
  10.  * Revision 1.9  1994/06/30  17:57:10  mjl
  11.  * All C example programs: made another pass to eliminate warnings when using
  12.  * gcc -Wall.  Lots of cleaning up: got rid of includes of math.h or string.h
  13.  * (now included by plplot.h), eliminated redundant casts, put in more
  14.  * uniform comments, and other minor changes.
  15.  *
  16.  * Revision 1.8  1994/03/30  07:21:44  mjl
  17.  * Changes to all C example programs: special handling for malloc re: header
  18.  * files eliminated, include of stdio.h and stdlib.h eliminated (now done
  19.  * by plplot.h), include of "plplot.h" changed to <plplot.h> to enable
  20.  * simpler builds by the general user, some cleaning up also.
  21. */
  22.  
  23. /*    x01c.c
  24.  
  25.     Simple line plot and multiple windows demo.
  26. */
  27.  
  28. #include <plplot.h>
  29.  
  30. /* Variables and data arrays used by plot generators */
  31.  
  32. static PLFLT x[101], y[101];
  33. static PLFLT xscale, yscale, xoff, yoff, xs[6], ys[6];
  34. static PLCursor plc;
  35.  
  36. /* Function prototypes */
  37.  
  38. void plot1(void);
  39. void plot2(void);
  40. void plot3(void);
  41.  
  42. /*----------------------------------------------------------------------*\
  43.  * main
  44.  *
  45.  * Generates several simple line plots.  Demonstrates:
  46.  *   - subwindow capability
  47.  *   - setting up the window, drawing plot, and labelling
  48.  *   - changing the color
  49.  *   - automatic axis rescaling to exponential notation
  50.  *   - placing the axes in the middle of the box
  51.  *   - gridded coordinate axes
  52. \*----------------------------------------------------------------------*/
  53.  
  54. int
  55. main(int argc, char *argv[])
  56. {
  57.     PLINT digmax;
  58.     char ver[80];
  59.     int co;
  60.  
  61. /* plplot initialization */
  62. /* Divide page into 2x2 plots unless user overrides */
  63.  
  64.     plssub(2, 2);
  65.  
  66. /* Parse and process command line arguments */
  67.  
  68.     (void) plParseInternalOpts(&argc, argv, PL_PARSE_FULL);
  69.  
  70. /* Get version number, just for kicks */
  71.  
  72.     plgver(ver);
  73.     printf("Plplot library version: %s\n", ver);
  74.  
  75. /* Initialize plplot */
  76.  
  77.     plinit();
  78.  
  79. /* Set up the data */
  80. /* Original case */
  81.  
  82.     xscale = 6.;
  83.     yscale = 1.;
  84.     xoff = 0.;
  85.     yoff = 0.;
  86.  
  87. /* Do a plot */
  88.  
  89.     plot1();
  90.  
  91. /* Set up the data */
  92.  
  93.     xscale = 1.;
  94.     yscale = 0.0014;
  95.     yoff = 0.0185;
  96.  
  97. /* Do a plot */
  98.  
  99.     digmax = 5;
  100.     plsyax(digmax, 0);
  101.     plot1();
  102.  
  103.     plot2();
  104.  
  105.     plot3();
  106.  
  107. /* Let's get some user input */
  108.  
  109.     do {
  110.     co = 0;
  111.     if (plGetCursor(&plc)) {
  112.         pltext();
  113.         printf(" wx = %f\n",plc.wX);
  114.         printf(" wy = %f\n",plc.wY);
  115.         printf(" vx = %f\n",plc.vpX);
  116.         printf(" vy = %f\n",plc.vpY);
  117.         plgra();
  118.         co = 1;
  119.     }
  120.     } while (co);
  121.  
  122. /* Don't forget to call PLEND to finish off! */
  123.  
  124.     plend();
  125.     exit(0);
  126. }
  127.  
  128.  /* =============================================================== */
  129.  
  130. void
  131. plot1(void)
  132. {
  133.     int i;
  134.     PLFLT xmin, xmax, ymin, ymax;
  135.  
  136.     for (i = 0; i < 60; i++) {
  137.     x[i] = xoff + xscale * (i + 1) / 60.0;
  138.     y[i] = yoff + yscale * pow(x[i], 2.);
  139.     }
  140.  
  141.     xmin = x[0];
  142.     xmax = x[59];
  143.     ymin = y[0];
  144.     ymax = y[59];
  145.  
  146.     for (i = 0; i < 6; i++) {
  147.     xs[i] = x[i * 10 + 3];
  148.     ys[i] = y[i * 10 + 3];
  149.     }
  150.  
  151. /* Set up the viewport and window using PLENV. The range in X is 
  152.  * 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes are 
  153.  * scaled separately (just = 0), and we just draw a labelled 
  154.  * box (axis = 0). 
  155.  */
  156.     plcol(1);
  157.     plenv(xmin, xmax, ymin, ymax, 0, 0);
  158.     plcol(2);
  159.     pllab("(x)", "(y)", "#frPLplot Example 1 - y=x#u2");
  160.  
  161. /* Plot the data points */
  162.  
  163.     plcol(4);
  164.     plpoin(6, xs, ys, 9);
  165.  
  166. /* Draw the line through the data */
  167.  
  168.     plcol(3);
  169.     plline(60, x, y);
  170. }
  171.  
  172.  /* =============================================================== */
  173.  
  174. void
  175. plot2(void)
  176. {
  177.     int i;
  178.  
  179. /* Set up the viewport and window using PLENV. The range in X is -2.0 to
  180.  * 10.0, and the range in Y is -0.4 to 2.0. The axes are scaled separately
  181.  * (just = 0), and we draw a box with axes (axis = 1). 
  182.  */
  183.     plcol(1);
  184.     plenv(-2.0, 10.0, -0.4, 1.2, 0, 1);
  185.     plcol(2);
  186.     pllab("(x)", "sin(x)/x", "#frPLplot Example 1 - Sinc Function");
  187.  
  188. /* Fill up the arrays */
  189.  
  190.     for (i = 0; i < 100; i++) {
  191.     x[i] = (i - 19.0) / 6.0;
  192.     y[i] = 1.0;
  193.     if (x[i] != 0.0)
  194.         y[i] = sin(x[i]) / x[i];
  195.     }
  196.  
  197. /* Draw the line */
  198.  
  199.     plcol(3);
  200.     plline(100, x, y);
  201. }
  202.  
  203.  /* =============================================================== */
  204.  
  205. void
  206. plot3(void)
  207. {
  208.     PLINT space0 = 0, mark0 = 0, space1 = 1500, mark1 = 1500;
  209.     int i;
  210.  
  211. /* For the final graph we wish to override the default tick intervals, and
  212.  * so do not use PLENV 
  213.  */
  214.     pladv(0);
  215.  
  216. /* Use standard viewport, and define X range from 0 to 360 degrees, Y range
  217.  * from -1.2 to 1.2. 
  218.  */
  219.     plvsta();
  220.     plwind(0.0, 360.0, -1.2, 1.2);
  221.  
  222. /* Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y. */
  223.  
  224.     plcol(1);
  225.     plbox("bcnst", 60.0, 2, "bcnstv", 0.2, 2);
  226.  
  227. /* Superimpose a dashed line grid, with 1.5 mm marks and spaces. 
  228.  * plstyl expects a pointer!! 
  229.  */
  230.     plstyl(1, &mark1, &space1);
  231.     plcol(2);
  232.     plbox("g", 30.0, 0, "g", 0.2, 0);
  233.     plstyl(0, &mark0, &space0);
  234.  
  235.     plcol(3);
  236.     pllab("Angle (degrees)", "sine", "#frPLplot Example 1 - Sine function");
  237.  
  238.     for (i = 0; i < 101; i++) {
  239.     x[i] = 3.6 * i;
  240.     y[i] = sin(x[i] * 3.141592654 / 180.0);
  241.     }
  242.  
  243.     plcol(4);
  244.     plline(101, x, y);
  245. }
  246.